import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'
const SnKey = 'MonitorSn'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

// 生成15位SN：TEST20200702001，后面三位是序列号
export function getNextSn() {
  let today = new Date();
  // yyyyMMdd
  today = today.getFullYear() + '' + pad(today.getMonth() + 1) + '' + pad(today.getDate());

  let item = localStorage.getItem(SnKey);
  let index = 1;
  if (item) {
    let day = item.substring(0, 8);

    if (day != today) {
      item = today + '001' + 'TEST';
    } else {
      item = (parseInt(item.substring(0, item.length - 4)) + 1) + 'TEST';
    }
  } else {
    item = today + '001' + 'TEST';
  }

  localStorage.setItem(SnKey, item)

  return item;
}

function pad(number) {
  if ( number < 10 ) {
    return '0' + number;
  }
  return number;
}
